<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_we_article_type".
 *
 * @property integer $id
 * @property integer $weid
 * @property string $typename
 * @property integer $parentid
 * @property string $addtime
 * @property integer $sortorder
 * @property integer $isnew
 * @property string $imagepath
 * 
 * 
 */
class GkyWeArticleType extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_we_article_type';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['weid', 'isnew'], 'integer'],
            [['typename'], 'string', 'max' => 64],
//            [['imagepath'], 'string', 'max' => 255],
            [['addtime'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'weid' => '订阅号',
            'typename' => '栏目名称',
            'parentid' => '父级编号',
            'addtime' => '添加时间',
            'sortorder' => '排序',
            'isnew' => '是否为新增栏目',
            'imagepath' => '栏目图标地址',
        ];
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {

//            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
//            if(isset(Yii::$app->user->identity->id)){
//                $id = Yii::$app->user->identity->id;
//                //添加人的id
//                $this->addmanid = $id;
//                //添加人的姓名
//                $name = GkyUser::find()->where(['id' => $id])->one()->username;
//                $this->addman = $name;
//            }
            //添加时间、发送时间
            $time = date("Y-m-d H:i:s", time());
            $this->addtime = $time;


            $path = Yii::$app->request->post('files');
            $filestatus = Yii::$app->request->post('fileStatus');
            for ($i = 0; $i < count($filestatus); $i++) {
                if ($filestatus[$i] == 1) {
                    $this->imagepath = $path[$i];
//                    $this->save();
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getParent() {
        return $this->hasOne(GkyWeArticleType::className(), ['id' => 'parentid']);
    }

}
